﻿using Microsoft.AspNetCore.Mvc;

namespace NLS.API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class NlsTTSController : Controller
    {
        NlsTTSClient nlsTTSClient;

        public NlsTTSController(NlsTTSClient nLS)
        {
            this.nlsTTSClient = nLS;
        }

        /// <summary>
        ///     语音合成
        /// </summary>
        /// <param name="text">待合成的文本，需要为UTF-8编码。使用GET方法，需要再采用RFC 3986规范进行urlencode编码；使用POST方法不需要urlencode编码。</param>
        /// <param name="format">音频编码格式，支持pcm/wav/mp3格式。默认值：mp3。</param>
        /// <param name="sample_rate">音频采样率，支持16000 Hz和8000 Hz，默认值：16000 Hz。</param>
        /// <param name="voice">发音人,更多发音人请参见接口说明。</param>
        /// <param name="volume">音量，取值范围：0~100</param>
        /// <param name="speech_rate">语速，取值范围：-500~500</param>
        /// <param name="pitch_rate">语调，取值范围：-500~500</param>
        /// <returns></returns>
        public async Task<IActionResult> TTS(string text, string? format = "mp3", int? sample_rate = null, string? voice = null, int? volume = null, int? speech_rate = null, int? pitch_rate = null)
        {
            try
            {
                var (bytes, mediaType) = await nlsTTSClient.Post(text, format, sample_rate, voice, volume, speech_rate, pitch_rate);
                return File(bytes, mediaType);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

    }
}
